package quantri.login;

import java.util.ArrayList;
import java.util.HashMap;

import dbutil.ConnectDB;
/******************************************
 * Filename			: ComponentNavigator.java
 * Decription		: Bean chứa danh sách các chức năng
 * Created date		: Jun 2, 2008
 * Author			: Phonglt
 ******************************************/
public class ComponentNavigator {
	private ArrayList<Group> groups;
	private ArrayList<HashMap<String,Object>> data;

	/**************************************
	 * Method name		: updateDataSrc
	 * Return type		: void
	 * Decription		:
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 **************************************/
	void updateDataSrc(String idThanhVien){
		if ("".equals(idThanhVien)) {
			return;
		}
		String SQL = "SELECT B.groupid , C.fullname as groupname, C.folder as groupfolder, B.catid , D.fullname as catname, B.fullname, B.folder " +
				"FROM dschucnang B, nhomchucnang C, loaichucnang D " +
				"WHERE groupid in " +
				"(SELECT A.gid FROM quyenchucnang A WHERE A.idthanhvien = '" 
				+ idThanhVien + "') " +
				"AND B.groupid = C.id AND B.catid = D.id " +
				"ORDER BY groupid, catid";
		ArrayList<HashMap<String,Object>> array = ConnectDB.getDataTypeList(SQL);
		
		this.data = array;
		this.fillData();
	}
	
	/**************************************
	 * Method name		: fillData
	 * Return type		: void
	 * Decription		: chuyển đổi dữ liệu từ DB thành đối tượng Groups
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 **************************************/
	void fillData() {
		ArrayList<Group> groups = new ArrayList<Group>();
		for (int i = 0; i < this.data.size(); i++) {
			HashMap<String, Object> row = this.data.get(i);
			Function newFunc = new Function((String) row.get("fullname"),
					((String) row.get("folder")).trim());
			Category newCat = new Category((Integer) row.get("catid"),
					(String) row.get("catname"));
			Group newGroup = new Group((Integer) row.get("groupid"),
					(String) row.get("groupname"), ((String) row
							.get("groupfolder")).trim());
			if (i == 0) {
				//truong hop them mot group hoan toan moi
				newCat.addFunction(newFunc);
				newGroup.addCategory(newCat);
				groups.add(newGroup);
			} else {
				Group lastGroup = groups.get(groups.size() - 1);
				if (lastGroup.getId() != newGroup.getId()) {
					groups.add(newGroup);
					lastGroup = newGroup;
				} 
				lastGroup.addMatchCat(newCat);
				lastGroup.addMatchFunc(newFunc);
			}
		}
		this.groups = groups;
	}
	/****************************************************
	 * Method name		: getGroups [GETTER]
	 * Return type		: ComponentNavigator
	 * Decription		: 
	 * Created date		: Jun 4, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public ArrayList<Group> getGroups() {
		return groups;
	}
	/****************************************************
	 * Method name		: setGroups [SETTER]
	 * Return type		: void
	 * Decription		: 
	 * Created date		: Jun 4, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setGroups(ArrayList<Group> groups) {
		this.groups = groups;
	}
	/****************************************************
	 * Method name		: getData [GETTER]
	 * Return type		: ComponentNavigator
	 * Decription		: 
	 * Created date		: Jun 4, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public ArrayList<HashMap<String, Object>> getData() {
		return data;
	}
	/****************************************************
	 * Method name		: setData [SETTER]
	 * Return type		: void
	 * Decription		: 
	 * Created date		: Jun 4, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setData(ArrayList<HashMap<String, Object>> data) {
		this.data = data;
	}
	
	
}
